package com.naiterui.ehp.bs.pharmacist.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.esign.ESignUtils;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.feign.PharmacistEsignFeignClient;
import com.naiterui.ehp.bs.pharmacist.service.IPharmacistService;
import com.naiterui.ehp.bs.pharmacist.service.IUserService;
import com.naiterui.ehp.bs.pharmacist.vo.esign.ESignConfigVO;
import com.naiterui.ehp.bs.pharmacist.vo.esign.YwxAppConfigVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.GloableConfigVo;
import com.naiterui.ehp.bs.pharmacist.vo.user.TencentCloudConfigVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.UserAuthDetailVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.UserDetailInfoVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Description 用户信息接口
 * @Author gongbaoqiang
 * @Date 2019/12/3 12:32 PM
 */
@Slf4j
@Api(description = "用户信息接口", tags = SwaggerTag.PHARMACIST + SwaggerTag.USER_INFO)
@RequestMapping("pharmacist/user")
@RestController
public class PharmacistUserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private PharmacistEsignFeignClient esignFeginClient;

    @Autowired
    private IPharmacistService pharmacistService;

    @ApiOperation("个人中心")
    @PostMapping("center")
    public ResponseVO<UserInfoVO> center(Long pharmacistId) throws BusinessException {
        log.info("个人中心主页: pharmacistId={}", pharmacistId);
        return PublicService.returnResponseVO(userService.getUserInfo(pharmacistId));
    }

    @ApiOperation("我的信息")
    @PostMapping("detail")
    public ResponseVO<UserDetailInfoVO> detail(Long pharmacistId) throws BusinessException {
        log.info("我的详细信息：pharmacistId={}", pharmacistId);
        return PublicService.returnResponseVO(userService.getUserDetail(pharmacistId));
    }

    /**
     * <获取实名认证验证码>
     * @param pharmacistId
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @PostMapping("realname/code")
    @ApiOperation(value = "获取实名认证验证码", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pharmacistId", value = "药师id", required = true, dataType = "long") })
    public ResponseVO<Void> realnameCode(Long pharmacistId) throws BusinessException {

        log.info("获取实名认证验证码 请求参数：pharmacistId = [{}]", pharmacistId);

        userService.getRealnameCode(pharmacistId);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);

    }

    /**
     * <实名认证验证码>
     * @param pharmacistId
     * @return
     * @throws BusinessException
     */
    @ResponseBody
    @PostMapping("realname/check")
    @ApiOperation(value = "实名认证", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pharmacistId", value = "药师ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string") })
    public ResponseVO<Void> realnameCheck(Long pharmacistId, String code) throws BusinessException {

        log.info("实名认证 请求参数：pharmacistId = [{}], code={}", pharmacistId, code);

        userService.checkRealnameCode(pharmacistId, code);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);

    }



    /**
     * 药师首页全局配置参数
     * @param pharmaistId
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "取首页全局配置参数", notes = "获取首页全局配置参数", httpMethod = "POST")
    @RequestMapping("globalConfig")
    @ResponseBody
    public ResponseVO<GloableConfigVo> globalConfig(Long pharmaistId) throws BusinessException {
        GloableConfigVo globleConfigVO = new GloableConfigVo();
        //签名配置
        ESignConfigVO eSignConfigVO = new ESignConfigVO();
        eSignConfigVO.setSeviceName(ESignUtils.getServiceId());
        eSignConfigVO.setSealType(ESignUtils.getSealType());
        globleConfigVO.setESignConfigVO(eSignConfigVO);

        //医网信签名账号配置
        YwxAppConfigVO ywxAppConfig = new YwxAppConfigVO();
        Map<String, String> signMap = esignFeginClient.getYwxSignConfig();
        ywxAppConfig.setClientId(signMap.get("clientId"));
        ywxAppConfig.setSecret(signMap.get("secret"));
        globleConfigVO.setBjcaSignConfig(ywxAppConfig);

        //腾讯云人脸识别账号配置
        TencentCloudConfigVO tencentCloudConfig = new TencentCloudConfigVO();
        tencentCloudConfig.setAppId(ConfigUtil.getString("tencent-cloud.pharmacist.wbAppId"));
        tencentCloudConfig.setLicence(ConfigUtil.getString("tencent-cloud.pharmacist.wbLicence"));
        globleConfigVO.setTencentCloudConfig(tencentCloudConfig);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, globleConfigVO);
    }

    @ApiOperation("获取认证详情")
    @GetMapping("getAuthDetail")
    public ResponseVO<UserAuthDetailVO> getAuthDetail(@RequestParam Long pharmacistId, @RequestParam String uuid)
        throws BusinessException {
        log.info("获取认证详情：pharmacistId={}, uuid={}", pharmacistId, uuid);
        return PublicService.returnResponseVO(userService.getUserAuthDetail(pharmacistId, uuid));
    }

    /**
     * 修改药师签章图片
     * 
     * @param pharmacistId
     * @param sealImg
     * @return
     * @throws BusinessException
     */
    @PostMapping("saveSealImg")
    @ApiOperation(value = "修改药师签章图片", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseVO<String> saveSealImg(@RequestParam("pharmacistId") Long pharmacistId,
        @RequestParam("sealImg") MultipartFile sealImg) throws BusinessException {
        log.info("修改药师签章图片, doctorId {}, sealImg {}", pharmacistId, sealImg);
        return PublicService.returnResponseVO(pharmacistService.saveSealImg(pharmacistId, sealImg));
    }
}
